﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.Product;
using NTTHAO.CommonWeb;

public partial class admin_webparts_Plug_ins_Product_ProductTypeMove : System.Web.UI.UserControl
{
    public ProductType _Type = null;
    public string _ErrorMsg = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            _ErrorMsg = "";
            this.btnSave.Text = LanguageUtility.GetLanguageText("SAVE", Session);

            int typeID = Convert.ToInt32(Request.QueryString["typeid"].Trim());
            _Type = new ProductType(typeID);
            if (_Type.ID < 1)
            {
                throw new Exception("This content does not exist.");
            }

            if (!IsPostBack)
            {
                txtID.Text = typeID.ToString("0");
                txtName.Text = _Type.Name;
            }
        }
        catch 
        {
            Response.Redirect(_ReturnUrl, false);
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            int toID = Convert.ToInt32(txtToID.Text.Trim());

            if (toID > 0)
            {
                ProductType toType = new ProductType(toID);
                if (toType.RelatedTypeID.Contains(_Type.RelatedTypeID + _Type.ID.ToString("0") + ";"))
                {
                    _ErrorMsg = "Không thể di chuyển nhóm sản phẩm vào nhóm con của chính nó.";
                    return;
                }
            }

            this.MoveType(_Type, toID);
            Response.Redirect("ProductContent.aspx?action=reloadtop", false);
            return;
        }
        catch (Exception ex)
        {
            _ErrorMsg = ex.Message + "\\n" + ex.Source + "|" + txtToID.Text;
        }
    }

    protected void MoveType(ProductType type, int moveToTypeID)
    {
        type.MoveTo(moveToTypeID);
    }

    public string _ReturnUrl
    {
        get
        {
            return "?action=view&g=" + Request.QueryString["g"] + "&typeid=" + Request.QueryString["typeid"];
        }
    }
}
